<template>
  <div style="margin-top: 20px">
    <el-row>
      <el-col :span=5>
        <el-input prefix-icon="el-icon-search" placeholder="请输入标签名字" v-model="searchInfo"></el-input>
      </el-col>
      <el-col :span="1" :offset=2>
        <el-button type="primary" @click="search">搜索</el-button>
      </el-col>
      <el-col :span="4" :offset="12">
        <el-button type="success" @click="addTags">添加</el-button>
      </el-col>
    </el-row>
  </div>
  <div style="margin: 20px 0">
    <el-table v-loading="loading" :data="tags" style="width: 100%" border stripe>
      <el-table-column prop="id" align="center" width="50">
      </el-table-column>
      <el-table-column prop="name" label="标签名" align="center" width="200"></el-table-column>
      <el-table-column prop="count" label="文章数量" align="center" width="100"></el-table-column>
      <el-table-column label="颜色" align="center" width="50" >
        <template #default="scope">
          <div style="width: 30px;height: 30px" :style="{'background':scope.row.color}"></div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button size="mini" type="primary" @click="Edit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="Delete(scope.$index,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="countStyle">
    <el-pagination background hide-on-single-page layout="prev,pager,next" :page-count="pages" v-model:current-page="currentPage">
    </el-pagination>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: "Types",
  data(){
    return{
      currentPage: 0,
      pages: 0,
      loading: true,
      searchInfo: '',
      tags:[],
      deleteTag: {
        id: null,
        md5Password: null
      }
    }
  },
  watch: {
    currentPage(value){
      this.loading = true
      this.getTags(value)
    }
  },
  mounted(){
    this.getTags(1);
  },
  methods: {
    search: function (){
      axios.get("/admin/searchTags",{params:{searchInfo:this.searchInfo}}).then( (res) => {
        this.tags = res.data;
      })
    },
    getTags: function (currentPage){
      axios.get("/admin/listTags",{params:{currentPage:currentPage,pageSize:10}}).then( (res) => {
        this.currentPage = res.data.current;
        this.pages = res.data.pages;
        this.tags = res.data.records;
        this.loading = false;
      })
    },
    addTags: function (){
      this.$router.replace({name:'addTags'});
    },
    Edit: function (id){
      this.$router.push({
        name: 'addTags',
        query: {
          id: id
        }
      })
    },
    Delete: function (index,row){
      this.$confirm('此操作不可逆,是否继续','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        text: 'warning'
      }).then(() => {
        this.tags.splice(index,1);
        this.deleteTag.id = row.id
        this.deleteTag.md5Password = localStorage.getItem("password");
        axios.post("/admin/deleteTag",this.deleteTag).then( (res) => {
          if ( res.data === true){
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          }
          else
            this.$message.error("删除失败");
        })

      }).cache(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>

</style>